﻿using System.ComponentModel.DataAnnotations;

namespace Lanbt.Core.Web.Filter.Validate
{
    /// <summary>
    /// 验证数组集合数量不小于0
    /// </summary>
    public class ArrayNonzeroAttribute : ValidationAttribute
    {

        public override bool IsValid(object? value)
        {
            if(value == null)
            {
                return false;
            }

            if (value.GetType() == typeof(int[]))
            {
                return ((int[])value).Count() > 0;
            }
            else if (value.GetType() == typeof(string[]))
            {
                return ((string[])value).Count() > 0;
            }
            else if (value.GetType() == typeof(IList<int>))
            {
                return ((IList<int>)value).Count() > 0;
            }
            else if (value.GetType() == typeof(IList<string>))
            {
                return ((IList<string>)value).Count() > 0;
            }
            else if (value.GetType() == typeof(List<int>))
            {
                return ((List<int>)value).Count() > 0;
            }
            else if (value.GetType() == typeof(List<string>))
            {
                return ((List<string>)value).Count() > 0;
            }

            return false;
        }
    }
}
